﻿using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage;
using ProduceBasicData.Api.DTO.ProductionManage;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage
{
    public class GetWorkCommandHandler : IRequestHandler<GetWorkCommand, ApiPaging<WorkOrderDto>>
    {
        /// <summary>
        /// 工单
        /// </summary>
        private readonly IBaseRespository<Work_order> orderRespository;
        /// <summary>
        /// 客户
        /// </summary>
        private readonly IBaseRespository<ClientModel> clientRespository;
        /// <summary>
        /// 产线
        /// </summary>
        private readonly IBaseRespository<ProductionModel> productionRespository;
        /// <summary>
        /// bom
        /// </summary>
        private readonly IBaseRespository<BOMInfoModel> BomRespository;
        /// <summary>
        /// 车间
        /// </summary>
        private readonly IBaseRespository<Workshopmodel> workshopmodel;

        public GetWorkCommandHandler(IBaseRespository<Work_order> orderRespository,
            IBaseRespository<ClientModel> clientRespository,
            IBaseRespository<ProductionModel> productionRespository,
            IBaseRespository<BOMInfoModel> BomRespository,
            IBaseRespository<Workshopmodel> workshopmodel)
        {
            this.orderRespository = orderRespository;
            this.clientRespository = clientRespository;
            this.productionRespository = productionRespository;
            this.BomRespository = BomRespository;
            this.workshopmodel = workshopmodel;
        }

        public Task<ApiPaging<WorkOrderDto>> Handle(GetWorkCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<WorkOrderDto>()
            {
                Code = ApiEnum.Success,
                Msg = "获取生产工单成功"
            };
            var info = from a in orderRespository.GetAll()
                       join b in clientRespository.GetAll() on a.Customer_id equals b.Id
                       join c in productionRespository.GetAll() on a.Producttion_id equals c.Id
                       join d in BomRespository.GetAll() on a.Product_id equals d.Id
                       join e in workshopmodel.GetAll() on a.WorkshopId equals e.Id
                       where !a.Isdel && !b.Isdel && !c.Isdel && !d.Isdel && !e.Isdel
                       select new WorkOrderDto()
                       {
                           Id = a.Id,
                           Isdel = b.Isdel,
                           Order_no = a.Order_no,
                           Order_number = a.Order_number,
                           Customer_id = a.Customer_id,
                           Clientname = b.Clientname,
                           Work_type = a.Work_type,
                           Work_Desc = a.Work_Desc,
                           Producttion_id = a.Producttion_id,
                           Productioncode = c.Productioncode,
                           Productionname = c.Productionname,
                           Product_id = a.Product_id,
                           BOMCode = d.BOMCode,
                           BOMName = d.BOMName,
                           BOMDosage = d.BOMDosage,
                           WorkshopId=e.Id,
                           Workshopcode=e.Workshopcode,
                           Workshopname = e.Workshopname,
                           Start_time = a.Start_time,
                           End_time = a.End_time,
                           Status = a.Status,
                           Upload = a.Upload,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                       };
            if (request.Order_no != null)
            {
                info = info.Where(o => o.Order_no == request.Order_no);
            }
            if (!string.IsNullOrEmpty(request.Work_type))
            {
                info = info.Where(o => o.Work_type.Contains(request.Work_type));
            }
            if (request.Customer_id != 0)
            {
                info = info.Where(o => o.Customer_id == request.Customer_id);
            }
            if (request.Producttion_id != 0)
            {
                info = info.Where(o => o.Producttion_id == request.Producttion_id);
            }
            if (request.Product_id != 0)
            {
                info = info.Where(o => o.Product_id == request.Product_id);
            }
            if (request.WorkshopId != 0)
            {
                info = info.Where(o => o.WorkshopId == request.WorkshopId);
            }
            if (request.Status != 0)
            {
                info = info.Where(o => o.Status == request.Status);
            }
           
            int total = info.Count();
            result.TotalCount = info.Count();
            result.PageDatas = info.OrderByDescending(o => o.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            return Task.FromResult(result);
        }
    }
}
